import Vue from 'vue'
import VueRouter from 'vue-router'
// eslint-disable-next-line no-unused-vars
import Login from '../components/login'
// eslint-disable-next-line no-unused-vars
import Home from '../components/home'
// eslint-disable-next-line no-unused-vars
import welcome from '../components/welcome'
// eslint-disable-next-line no-unused-vars
import users from '../components/user/users'
// eslint-disable-next-line no-unused-vars
import rights from '../components/power/rights'
// eslint-disable-next-line no-unused-vars
import roles from '../components/power/roles'
// eslint-disable-next-line no-unused-vars
import cate from '../components/goods/cate'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    }, {
      path: '/',
      redirect: '/login'
    }, {
      path: '/home',
      component: Home,
      redirect: '/Welcome',
      children: [
        { path: '/Welcome', component: welcome },
        { path: '/users', component: users },
        { path: '/rights', component: rights },
        { path: '/roles', component: roles },
        { path: '/categories', component: cate }
      ]
    }
  ]
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from代表从哪个路径跳转而来
  // next是一个函数，表示放行
  // next():放行         next('/login'): 强制跳转
  console.log('------->' + to.path + '<------------')
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  console.log(tokenStr + '------')
  if (!tokenStr) return next('/login')
  next() // 放行
})
export default router
